﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using JelleDruyts.Windows;
using Microsoft.Win32;
using Schedulr.Infrastructure;
using Schedulr.Messages;
using Schedulr.Models;

namespace Schedulr.ViewModels
{
    public class QueuedPicturesViewModel : PictureListViewModel
    {
        protected override ObservableCollection<Picture> GetPicturesForAccount(Account account)
        {
            return account.QueuedPictures;
        }

        protected override bool IsDragDropSupportedOnQueue
        {
            get { return true; }
        }

        protected override IEnumerable<ICommand> GetQueueCommands()
        {
            return new RelayCommand[]
            {
                new RelayCommand(this.Add, this.CanAdd, "_Add...", "Adds pictures to the queue, each in its own batch [INS]", new KeyGesture(Key.Insert)),
                new RelayCommand(this.AddBatch, this.CanAddBatch, "Add _Batch...", "Adds pictures to the queue, all in the same batch [ALT-INS]", new KeyGesture(Key.Insert, ModifierKeys.Alt)),
                new RelayCommand(this.UploadSelection, this.CanUploadSelection, "_Upload...", "Uploads the currently selected pictures to Flickr"),
                new RelayCommand(this.UploadBatch, this.CanUploadBatch, "U_pload Batch...", "Uploads the next batch of pictures to Flickr"),
                this.RemoveCommand
            };
        }

        protected override bool ArePictureDetailsReadOnly
        {
            get { return false; }
        }

        #region Commands

        private bool CanAdd(object parameter)
        {
            return true;
        }

        private void Add(object parameter)
        {
            Add(false);
        }

        private bool CanAddBatch(object parameter)
        {
            return true;
        }

        private void AddBatch(object parameter)
        {
            Add(true);
        }

        private void Add(bool addToSingleBatch)
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;
            openFileDialog.Title = (addToSingleBatch ? "Please select the pictures to add as a single batch" : "Please select the pictures to add as individual batches");
            if (openFileDialog.ShowDialog() == true)
            {
                Messenger.Send<AddPicturesRequestedMessage>(new AddPicturesRequestedMessage(openFileDialog.FileNames, addToSingleBatch));
            }
        }

        private bool CanUploadSelection(object parameter)
        {
            return (this.SelectedPictures.Count > 0 && FlickrClient.IsOnline());
        }

        private void UploadSelection(object parameter)
        {
            Messenger.Send<UploadPicturesRequestedMessage>(new UploadPicturesRequestedMessage(this.SelectedPictures, this.Account, UploadPicturesRequestReason.Interactive));
        }

        private bool CanUploadBatch(object parameter)
        {
            return (this.PictureList.Count > 0 && FlickrClient.IsOnline());
        }

        private void UploadBatch(object parameter)
        {
            Messenger.Send<UploadPicturesRequestedMessage>(new UploadPicturesRequestedMessage(this.PictureList.GetLowestBatch(), this.Account, UploadPicturesRequestReason.Interactive));
        }

        #endregion
    }
}